<?php
namespace haibao\cms\data;

use \haibao\cms\model\filter\Tag as TagFilter;
class AmericanTag extends \haibao\cms\data\BaseMysql{
	
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\AmericanTag');
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE );
	}
	

	
	/**
	 * 根据标签id查找子级标签树（包含当前标签，当前标签为要查询的顶级标签）
	 * @param \haibao\cms\model\data\Tag $model
	 */
	public function getChildrenTreeByTagId($tagId){
		$tagInfo = $this->getTagById($tagId);
		$tagParentArr = $this->getParentsTagArr($tagId);
		$arrTree = $this->getTagTree($tagInfo->TopId);
		while (count($tagParentArr)){
			$model = array_pop($tagParentArr);
			if($model->ParentId == \haibao\cms\model\data\Tag::TOP_PARENT_ID && isset($arrTree[$model->Id]['children'])){
				$arrTree = $arrTree[$model->Id]['children'];
			}else{
				$arrTree = isset($arrTree[$model->Id]['children']) ? $arrTree[$model->Id]['children'] : array($model->Id => $arrTree[$model->Id]);
			}
		}
		if(is_array($arrTree) && $tagInfo->Id == key($arrTree)){
			return $arrTree;
		} else {
			$retTag = $tagInfo->toArray();
			$retTag['children'] = $arrTree;
			return array($tagInfo->Id => $retTag);
		}
	}
	/**
	 * 根据标签id查询标签信息
	 * @param int $tagId
	 * @param boolean $detail 只显示基本信息
	 */
	public function getTagById($tagId, $brief = true){
		$model = $this->getOneById($tagId);
		if($model && $model->Alias){
			$model->Alias = trim($model->Alias,',');
		}
		return $model;
	}
	
	/**
	 * 根据标签id查找父级标签树
	 * @param \haibao\cms\model\data\Tag $model
	 */
	public function getParentsTagArr($tagId){
		$tagInfo = $this->getTagById($tagId);
		if($tagInfo){
			$this->_tagDictArr[$tagId] = $tagInfo;
			if($tagInfo->ParentId != \haibao\cms\model\data\Tag::TOP_PARENT_ID){
				$this->getParentsTagArr($tagInfo->ParentId);
			}
		}
		return $this->_tagDictArr;
	}
	
	public function getTagTree($topId = null){
	
		$tagTree = array();
		$allTagDict = $this->getAllTagDict();
		foreach($allTagDict as $key=>$tag){
			if(isset($allTagDict[$tag[TagFilter::CONDITION_FIELD_NAME_PARENT_ID]])){
				$allTagDict[$tag[TagFilter::CONDITION_FIELD_NAME_PARENT_ID]]['children'][$tag[TagFilter::CONDITION_FIELD_NAME_ID]] = &$allTagDict[$tag[TagFilter::CONDITION_FIELD_NAME_ID]];
			}else{
				$tagTree[$tag[TagFilter::CONDITION_FIELD_NAME_ID]] = &$allTagDict[$tag[TagFilter::CONDITION_FIELD_NAME_ID]];
			}
		}
		return $tagTree;
	}
	
	private function getAllTagDict(){
		$arrDict = array();
		$sql = 'select * from hmeiju_tag where tag_status!= %s';
		$result = $this->query($sql, array(\haibao\cms\model\data\Tag::STATUS_DISABLE));
		while ($result->fetch_assoc()){
				
			$tagId = $result->getData('tag_id');
			$arrDict[$tagId] = array(
					TagFilter::CONDITION_FIELD_NAME_ID => intval($tagId),
					TagFilter::CONDITION_FIELD_NAME_TOPIC_ID => intval($result->getData('topic_id')),
					TagFilter::CONDITION_FIELD_NAME_ALIAS =>trim($result->getData('alias'),'，'),
					TagFilter::CONDITION_FIELD_NAME_TAG_NAME => $result->getData('tag_name'),
					TagFilter::CONDITION_FIELD_NAME_PARENT_ID => intval($result->getData('parent_id')),
					TagFilter::CONDITION_FIELD_NAME_TOP_ID => intval($result->getData('top_id')),
					TagFilter::CONDITION_FIELD_NAME_TAG_LEVEL => intval($result->getData('tag_level')),
					TagFilter::CONDITION_FIELD_NAME_URL => $result->getData('url'),
					TagFilter::CONDITION_FIELD_NAME_TAG_TYPE => $result->getData('tag_type'),
					TagFilter::CONDITION_FIELD_NAME_TAG_STATUS=>$result->getData('tag_status'),
					TagFilter::CONDITION_FIELD_NAME_QUANPIN=>$result->getData('quanpin'),
					TagFilter::CONDITION_FIELD_NAME_JIANPIN=>$result->getData('jianpin'),
					TagFilter::CONDITION_FIELD_NAME_CREATOR=>$result->getData('creator'),
					TagFilter::CONDITION_FIELD_NAME_CREATETIME=>$result->getData('created'),
					TagFilter::CONDITION_FIELD_NAME_SUSER=>$result->getData('suser'),
					TagFilter::CONDITION_FIELD_NAME_LASTMODIFIED=>$result->getData('lastmodified'),
					TagFilter::CONDITION_FIELD_NAME_DESC=>$result->getData('desc'),
					TagFilter::CONDITION_FIELD_NAME_ARTICLE_NUM=>$result->getData('article_num'),
					TagFilter::CONDITION_FIELD_NAME_HITS=>$result->getData('hits'),
					TagFilter::CONDITION_FIELD_NAME_TODAY_HITS=>$result->getData('today_hits'),
					TagFilter::CONDITION_FIELD_NAME_YESTODAY_HITS=>$result->getData('yestoday_hits'),
					TagFilter::CONDITION_FIELD_NAME_MOUTH_WEIGHT=>$result->getData('month_weight'),
			);
		}
		return $arrDict;
	}
	
}
